<?php

class Mojito_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
	private $Acl;
	
	public function __construct(Zend_Acl $Acl)
	{
		$this->Acl = $Acl;
		
	}
	
	public function preDispatch(Zend_Controller_Request_Abstract $Request)
	{
		if ($this->Acl->has($Request->getParam('controller')))
		{
			$Auth = Zend_Auth::getInstance();
			if ($Auth->hasIdentity()) $role = $Auth->getIdentity()->role;
			else $role = 'guest';

			if (!$this->Acl->isAllowed($role,$Request->getParam('controller'),$Request->getParam('action')))
			{
				Zend_Controller_Action_HelperBroker::addPrefix('Mojito_Controller_Action_Helper');
				Zend_Controller_Action_HelperBroker::getStaticHelper('RestResponse')->denied()->appendBody('Access Denied')->sendResponse();
				exit();
			}
		}
	}	
}